package com.ljkj.blockpay.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.ljkj.blockpay.R;
import com.ljkj.blockpay.util.KeyboardUtil;

public class TitleView extends RelativeLayout {
    private Context mContext;
    private ImageView mLeftBack;
    public TextView mContentTitle;
    private ImageView mIvRightMore;
    private TextView mRightText;

    public TitleView(Context context) {
        super(context);
        this.mContext = context;
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    /**
     * 设置titleView返回键隐藏
     */
    public void hideLeftImgBtn(boolean bo) {
        if (bo) {
            mLeftBack.setVisibility(GONE);
        } else {
            mLeftBack.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置title文字
     *
     * @param title
     */
    public void setTitleContent(int title) {
        //设置粗体
        mContentTitle.setText(title);
    }

    public void setRightText(int title){
        mRightText.setText(title);
    }

    public void setTitleColor(int color) {
        mContentTitle.setTextColor(ContextCompat.getColor(mContext, color));
    }

    public void setRightColor(int color){
        mRightText.setTextColor(ContextCompat.getColor(mContext, color));
    }

    /**
     * 设置右侧图片
     *
     * @param res
     */
    public void setRightImage(int res) {
        //设置粗体
        mIvRightMore.setVisibility(VISIBLE);
        mIvRightMore.setImageResource(res);
    }

    /**
     * 隐藏右侧图片
     */
    public void hideRightImgBtn(boolean bo) {
        if (bo) {
            mIvRightMore.setVisibility(GONE);
        } else {
            mIvRightMore.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置左侧按钮点击关闭
     *
     * @param activity
     */
    public void setLeftBackFinish(final Activity activity) {
        mLeftBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyboardUtil.hideKeyboard(activity);
                activity.finish();
            }
        });
    }

    /**
     * 设置左侧 图片资源样式
     *
     * @param res
     */
    public void setLeftImgBtn(int res) {
        mLeftBack.setImageResource(res);
    }

    /**
     * 设置左侧点击按钮事件
     *
     * @param listener
     */
    public void setLeftBtnListener(OnClickListener listener) {
        mLeftBack.setOnClickListener(listener);
    }

    /**
     * 设置右侧点击按钮事件
     *
     * @param listener
     */
    public void setRightBtnListener(OnClickListener listener) {
        mIvRightMore.setOnClickListener(listener);
    }

    /**
     * 设置右边文字
     * @param listener
     */
    public void setRightTextListener(OnClickListener listener){
        mRightText.setOnClickListener(listener);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.view_common_title, this, true);
        mLeftBack = view.findViewById(R.id.iv_left_back);
        mContentTitle = view.findViewById(R.id.content_title);
        mRightText = view.findViewById(R.id.tv_righttext);
        mIvRightMore = view.findViewById(R.id.iv_right_more);
    }
}
